package cn.xyz.it.Controller;

import cn.xyz.it.CommonUtil.DateUtils;
import cn.xyz.it.ExcelUtil.excel.ExportExcel;
import cn.xyz.it.po.JaxJournalTalkback;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @Description
 * @Author MissYu
 * @Date 2020/11/12 2:26
 * @Classname HelloController
 */
@RestController
@RequestMapping("/index")
public class HelloController {

    private static final Logger log = LoggerFactory.getLogger(HelloController.class);


    @RequestMapping("/get")
    public String get() {
        return "您好!";
    }


    /**
     * 下载模板
     *
     * @param response
     */
    @GetMapping("/download")
    public void download(HttpServletResponse response) {
        String fileName = "对讲日志模板" + DateUtils.convert(new Date()) + ".xlsx";
        List<JaxJournalTalkback> list = Lists.newArrayList();
        JaxJournalTalkback JaxJournalTalkback = new JaxJournalTalkback();
        JaxJournalTalkback.setId(1);
        JaxJournalTalkback.setCarno("赣A99886");
        JaxJournalTalkback.setDepid(1233563233);
        JaxJournalTalkback.setCategory("危货_汽油");
        JaxJournalTalkback.setFoulplay("超车");
        JaxJournalTalkback.setWhetherflag(0);
        JaxJournalTalkback.setAlertconsole("提醒内容");
        JaxJournalTalkback.setBegintime(new Date());
        JaxJournalTalkback.setEndtime(new Date());
        JaxJournalTalkback.setObjectman("李荣浩");
        JaxJournalTalkback.setTalkvideo("www.baidu.com");
        JaxJournalTalkback.setMonitorpeople("郭碧婷");
        list.add(JaxJournalTalkback);
        try {
            new ExportExcel("对讲日志", JaxJournalTalkback.class, 2).setDataList(list).write(response, fileName).dispose();
        } catch (IOException e) {
            e.printStackTrace();
            log.error("下载模板数据失败");
        }
    }


    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        String fileName = "测试模板" + DateUtils.convert(new Date()) + ".xlsx";
        List<JaxJournalTalkback> list = Lists.newArrayList();
        for (int i = 0; i < 20; i++) {
            JaxJournalTalkback JaxJournalTalkback = new JaxJournalTalkback();
            JaxJournalTalkback.setId(i+1);
            JaxJournalTalkback.setCarno("赣A99886");
            JaxJournalTalkback.setDepid(1233563233);
            JaxJournalTalkback.setCategory("危货_汽油");
            JaxJournalTalkback.setFoulplay("超车");
            JaxJournalTalkback.setWhetherflag(0);
            JaxJournalTalkback.setAlertconsole("提醒内容");
            JaxJournalTalkback.setBegintime(new Date());
            JaxJournalTalkback.setEndtime(new Date());
            JaxJournalTalkback.setObjectman("李荣浩");
            JaxJournalTalkback.setTalkvideo("www.baidu.com");
            JaxJournalTalkback.setMonitorpeople("郭碧婷");
            list.add(JaxJournalTalkback);
        }
        try {
            new ExportExcel("测试模板", JaxJournalTalkback.class, 2).setDataList(list).write(response, fileName).dispose();
        } catch (IOException e) {
            e.printStackTrace();
            log.error("下载模板数据失败");
        }
    }


}
